<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleAddCardCreate"> 补昨日卡 </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                onClick: handleView.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
    <AddFormModal @register="addModal[0]" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { ref } from 'vue';
  import { useRoute } from 'vue-router';
  import { cardListApi } from '@/api/sys/sysData';
  const route = useRoute();
  import AddFormModal from './AddFormModal.vue';

  import FormModal from './FormModal.vue';
  import { columns, searchFormSchema } from './role.data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';

  const addModal = useModal();

  defineOptions({ name: 'userNoteList' });
  const { createMessage } = useMessage();
  const loading = ref(false);

  const userName = ref(route.query?.userName);
  if (userName) {
    searchFormSchema[0].defaultValue = userName;
  }
  //
  // console.log("---userName:",route.query,route.params)
  const [register, { openModal, closeModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '打卡记录列表',
    api: cardListApi,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    // beforeFetch(info) {
    //   info.keyword="123"
    //   console.log('beforeFetch', info);
    //    return info;
    // },

    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    // actionColumn: {
    //   width: 80,
    //   title: '操作',
    //   dataIndex: 'action',
    //   // slots: { customRender: 'action' },
    //   fixed: undefined,
    // },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleAddCardCreate() {
    addModal[1].openModal(true, {});
  }

  function format(seconds) {
    //秒转化为分钟格式
    let s = Math.trunc(seconds);
    let minInt = Math.floor(s / 60);
    let min = minInt >= 10 ? minInt : '00' + minInt;
    s -= 60 * min;
    let sec = s >= 10 ? s : '00' + s;
    return min + ':' + sec;
  }

  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
  function handleView(record: Recordable) {
    window.open(record.remoteUrl);
  }

  function handleDelete(record: Recordable) {
    //console.log(record);

    deleteApi(record)
      .then((res) => {
        createMessage.success(res.message);
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        reload();
      });
  }

  function handleSuccess() {
    reload();
  }
  const arrHeader = [
    {
      key: 'userName',
      value: '姓名',
    },
    {
      key: 'levelName',
      value: '职级',
    },
  ];

  function getHeadArr() {
    let arr = [];
    arrHeader.forEach((r) => {
      arr.push(r.value);
    });
    return arr;
  }

  function changeSubmitArr(results) {
    let data = {
      list: [],
    };
    let list = [];
    results.forEach((r) => {
      let json = {};
      for (let key in r) {
        arrHeader.forEach((e) => {
          if (e.value == key) {
            json[e.key] = r[key];
          }
        });
      }
      data.list.push(json);
    });
    return data;
  }
</script>
